import React, { Component } from 'react';
import {base} from 'nc-lightapp-front';
const { NCTable} = base;
let {sort,location}=NCTable;
import hoverContent from './ncTableHoverShowBtn.js';
//添加标题 div
function dealColumns(TableComponent){
    return class GLTable extends Component {
        constructor(props) {
            super(props);
        }

        render() {
            let {
                columns,
                isDealColumn=true,//
                isDealTitle=true,
                isDealSort=true,//排序功能
                ...others
            } = this.props;

            let oprColumn = null;

            if (isDealColumn) {
                // 浅拷贝一下，避免出问题
                columns = [...columns];
                let last;
                let loop=(columnDatas)=>{
                    columnDatas.forEach((item, index) => {
                        if (item.attrcode === "opr" || item.attrcode === 'operate') {
                            oprColumn = item;
                            columnDatas.splice(index, 1);
                            // totalColumns && totalColumns.splice(index, 1);
                        } else if(item.title&&isDealTitle){
                            item.title=<div>{item.title}</div>;
                        }else if(item.key&&item.dataIndex&&item.dataIndex==item.key&&!item.attrcode){
                            item.attrcode=item.dataIndex;
                        }else{
                            item.visible && (last = item);
                        }
                        if(item.children&&item.children.length>0){
                            loop(item.children);
                        }else{
                            if(isDealSort&&(item.attrcode&&item.attrcode!="checkbox"||item.key!="checkbox")){//排序设置
                                item.sortEnable=true;
                                // item.sorter=(param)=>{
                                    
                                // }
                            }
                        }
                    });
                }
                loop(columns);
            }
            let sortObj={
                mode:'single',
                backSource:false,
                sortFun:()=>{}
            };
            return (
                <TableComponent
                    sort={sortObj}
                    columns={columns}
                    {...others}
                />
            );
        }
    };
}
//添加鼠标悬浮显示行操作按钮
function addHoverShowBtn(TableComponent){
    return hoverContent(TableComponent);
}
//添加排序功能
function addSort(TableComponent){
    return sort(TableComponent);
}
//添加定位功能
function addLocation(TableComponent){
    return location(TableComponent);
}
//处理列
function GLTable(TableComponent){
    return class GLTable extends Component {
        constructor(props) {
            super(props);
        }

        render() {
            let {
                columns,
                isDealColumn=true,//
                isDealTitle=true,
                isDealSort=true,//排序功能
                isDealLocation=true,
                ...others
            } = this.props;
            let resultGLTable=TableComponent;
            if(isDealSort){
                resultGLTable=sort(resultGLTable);
            }
            if(isDealLocation){
                resultGLTable=location(resultGLTable);
            }
            let sortObj={
                mode:'single',
                backSource:false,
                sortFun:()=>{}
            };
            return (
                <resultGLTable
                    sort={sortObj}
                    columns={columns}
                    {...others}
                />
            );
        }
    };
}
export {dealColumns,addHoverShowBtn,addSort,addLocation,GLTable}